package com.codeoss.module.sale.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.service.impl.BaseService;
import com.codeoss.module.sale.dao.SaleQuotationLineMapper;
import com.codeoss.module.sale.model.SaleQuotationLine;
import com.codeoss.module.sale.service.ISaleQuotationLineService;
import com.codeoss.module.uom.service.IUomUomConversionService;

@Service
public class SaleQuotationLineService extends BaseService<SaleQuotationLineMapper, SaleQuotationLine>
		implements ISaleQuotationLineService {
	@Autowired
    private IUomUomConversionService uomUomConversionService;
	@Transactional
	public void insert(SaleQuotationLine saleQuotationLine) {
		beforeSave(saleQuotationLine,true);
		super.insert(saleQuotationLine);
	}
	@Transactional
	public void update(SaleQuotationLine saleQuotationLine) {
		beforeSave(saleQuotationLine,false);
		super.update(saleQuotationLine);
	}
	@Transactional
	protected void beforeSave(SaleQuotationLine saleQuotationLine,boolean isNewRecord) {
		Long productId=saleQuotationLine.getProductProductId();
	    BigDecimal qtyEntered=saleQuotationLine.getQtyEntered();
	    Long uomUomId=saleQuotationLine.getUomUomId();
	    if(productId!=null&&qtyEntered!=null&&uomUomId!=null) {
	    	BigDecimal qtyOrdered=uomUomConversionService.convert(productId, uomUomId, qtyEntered);
	    	saleQuotationLine.setQtyOrdered(qtyOrdered);
	    }
	}
	
	@Override
	public List<SaleQuotationLine> getLineListBySaleQuotationId(Long saleQuotationId) {
		// TODO Auto-generated method stub
		QueryWrapper<SaleQuotationLine> query=new QueryWrapper<SaleQuotationLine>();
		query.eq("sale_quotation_id", saleQuotationId);
		return list(query);
	}
}